<?php


namespace App\Http\Repositories\ProductCenter;


use App\Http\Repositories\BaseRepository;
use App\Models\ProductCenter\Models\BrandModel;
use App\Models\ProductCenter\Models\ShoesSizeModel;
use Illuminate\Support\Facades\DB;

class BrandRepository extends BaseRepository
{
    public $brand;
    public function __construct(BrandModel $brand)
    {
        $this->brand = $brand;
    }

    public function getBrandInfo($data)
    {
        $where['id'] = $data['id'];
        return $this->brand->getInfo($where);
    }

    public function getBrandList($data)
    {
        $where = [];
        is_real_exists(@$data['name']) && $where['name'] = ['like', '%'.$data['name'].'%'];
        is_real_exists(@$data['status']) && $where['status'] = $data['status'];

        switch ($data['operate']) {
            case 1:
                return $this->brand->getListByPage($where, ['*'], config('pageSize'));
            case 2:
                return $this->brand->getList($where);
            case 3:
                $field = 'id,id as value,parent_id,name,name as label';
                $brand = $this->brand->getList([], $field)->toArray();
                return $this->generateTree($brand);
            case 4:
                return $this->getBrandDownList($where);
            default:
                return [];
        }

    }

    public function addBrand($data)
    {
       $this->brand->addItem($data, true);

        return [];
    }

    public function editBrand($data)
    {
        $where['id'] = $data['id'];

        $this->brand->editItem($where, $data);

        return [];
    }

    public function delBrand($data)
    {
        return $this->brand->deleteItem($data);
    }

    public function getBrandDownList($where)
    {
//        $where['status'] = $this->brand::TRUE;
        $res = $this->brand
            ->handleCondition($where)
            ->orderBy('sort', 'desc')
            ->selectRaw('id,id as value,parent_id,name,name as label')
            ->get()
            ->toArray();

        return $this->generateTreeStr($res);
    }
}
